package activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.evaluity.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import data.DATA_Helper;
import fragments.AnswersListFragment;
import fragments.QuestionsListFragment;

import logic.Answer;
import logic.Question;
import logic.Test;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.RadioGroup;
import android.content.Context;
import android.content.Intent;

public class QuestionsList extends BaseActivity {
	
	public static List<Question> questions = new ArrayList<Question>();
	public static List<Answer> selectedAnswers = new ArrayList<Answer>();
	private HashMap<Integer, List<Answer>> answersByquestion = new HashMap<Integer, List<Answer>>();
	private int selectedQuestion;
	
	public static final String EXTRA_TEST_ID = "test_id";

	public QuestionsList(){
		super(R.string.left_and_right);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent i = getIntent();
		String testId = i.getStringExtra(EXTRA_TEST_ID);
		boolean offline = i.getBooleanExtra("offline", false);
		
		if (offline) {
			Test test = (Test)i.getSerializableExtra("test");
			questions = test.getQuestions();
			for (int i1 = 0; i1 < questions.size(); i1++) {
				answersByquestion.put(i1, questions.get(i1).getAnswers());
			}
		}
		
		else {
			questions = DATA_Helper.getQuestionsByTest(testId);
			for (int i1 = 0; i1 < questions.size(); i1++) {
				List<Answer> answers = DATA_Helper.getAnswersByQuestion(questions.get(i1).getId());
				questions.get(i1).setAnswers(answers);
				answersByquestion.put(i1, answers);
			}
		}
		
		
//		this.questions = test.getQuestions();
//		List<String> qNames = new ArrayList<String>();

//		for (int j = 0; j < questions.size(); j++) {
//			answersByquestion.put(j, questions.get(j).getAnswers());
//		}
		
//		this.selectedAnswers = answersByquestion.get(0);
		
		getSlidingMenu().setMode(SlidingMenu.RIGHT);
		getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

		setContentView(R.layout.content_frame);
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.content_frame, new QuestionsListFragment())
		.commit();

//		getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
//		getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
//		getSupportFragmentManager()
//		.beginTransaction()
//		.replace(R.id.menu_frame_two, new SampleListFragment())
//		.commit();
		
		setSlidingActionBarEnabled(false);
	}
	
	public void setSelectedQuestion(int position) {
		selectedAnswers = answersByquestion.get(position);
	}
	
	public void switchContent() {
//		mContent = fragment;
		AnswersListFragment f = new AnswersListFragment(this);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, f).commit();
		toggle();
		
//		getSlidingMenu().showContent(true);
		
//		toggle();
	}

	public void setSelectedAnswer(Answer answer) {
		for (Answer a : selectedAnswers) {
			if (a != answer)
				a.setSelected(false);
		}
	}
}
